package cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.multiple.flow;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.QuickWorkFlow;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskQuery;
import cn.schoolwow.workflow.domain.task.WorkFlowTaskResponse;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.start.StartAnalyzeWorkReportAuditFlow;
import cn.schoolwow.workflow.test.module.analyzeWorkReport.audit.single.start.SubmitAnalyzeWorkReportFlow;

import java.util.ArrayList;
import java.util.List;

public class MultipleAuditAnalyzeWorkReportFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        multipleStartWorkFlow(flowContext);
        halfPass(flowContext);
        halfReject(flowContext);
    }

    @Override
    public String name() {
        return "分析工作报告-工作流批量提交-批量审批";
    }

    private void multipleStartWorkFlow(FlowContext flowContext){
        List<WorkFlowInstance> workFlowInstanceList = new ArrayList<>();
        int total = flowContext.getData("total", int.class, 4);

        for(int i=0;i<total;i++){
            flowContext.startFlow("执行-分析工作报告-通过审批-测试用例")
                    .putTemporaryData("deployName", "分析工作报告审批")
                    .next(new StartAnalyzeWorkReportAuditFlow())
                    .next(new SubmitAnalyzeWorkReportFlow())
                    .execute();
            WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);
            workFlowInstanceList.add(workFlowInstance);
        }
        flowContext.putTemporaryData("workFlowInstanceList", workFlowInstanceList);
    }

    private void halfPass(FlowContext flowContext){
        List<WorkFlowInstance> workFlowInstanceList = flowContext.checkData("workFlowInstanceList", List.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        int total = flowContext.getData("total", int.class);

        int half = total/2;

        for(int i=0;i<half;i++){
            WorkFlowInstance workFlowInstance = workFlowInstanceList.get(i);

            WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
            workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
            //一级审批人同意
            {
                workFlowTaskQuery.assignerList.add("1");
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            }
            //二级审批人同意
            {
                workFlowTaskQuery.assignerList.clear();
                workFlowTaskQuery.assignerList.add("2");
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            }
        }
    }

    private void halfReject(FlowContext flowContext){
        List<WorkFlowInstance> workFlowInstanceList = flowContext.checkData("workFlowInstanceList", List.class);
        QuickWorkFlow quickWorkFlow = flowContext.checkInstanceData(QuickWorkFlow.class);
        int total = flowContext.getData("total", int.class);

        int half = total/2;
        for(int i=half;i<workFlowInstanceList.size();i++){
            WorkFlowInstance workFlowInstance = workFlowInstanceList.get(i);

            WorkFlowTaskQuery workFlowTaskQuery = new WorkFlowTaskQuery();
            workFlowTaskQuery.instanceIdList.add(workFlowInstance.getId());
            //一级审批人同意
            {
                workFlowTaskQuery.assignerList.add("1");
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.COMPLETE);
            }
            //二级审批人拒绝
            {
                workFlowTaskQuery.assignerList.clear();
                workFlowTaskQuery.assignerList.add("2");
                WorkFlowTaskResponse workFlowTaskResponse = quickWorkFlow.getSingleWorkFlowTask(workFlowTaskQuery);
                quickWorkFlow.completeTask(workFlowTaskResponse.taskId, TaskType.REJECT);
            }
        }
    }
}
